package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.found.common.constants.WxOpenServiceTypeConstant;
import com.ibeeking.found.common.entity.DictDTO;
import com.ibeeking.found.common.entity.open.*;
import com.ibeeking.found.common.enums.*;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.gmc.global.api.common.param.OpenAuthModifyTenantIdParam;
import com.ibeeking.gmc.global.api.common.param.OpenAuthParam;
import com.ibeeking.gmc.global.api.common.param.OpenAuthCancelParam;
import com.ibeeking.gmc.global.g.rest.service.IDictValueService;
import com.ibeeking.gmc.global.g.rest.service.IOpenAuthService;
import com.ibeeking.gmc.global.g.rest.service.IOpenConfigService;
import com.ibeeking.gmc.global.service.common.constants.enums.VerifyTypeInfoEnum;
import com.ibeeking.gmc.global.service.common.dos.OpenAuthDO;
import com.ibeeking.gmc.global.service.common.dos.OpenAuthFuncDO;
import com.ibeeking.gmc.global.service.common.dos.OpenConfigDO;
import com.ibeeking.gmc.global.service.common.vo.HasOpenAuthVO;
import com.ibeeking.gmc.global.service.mapper.OpenAuthFuncMapper;
import com.ibeeking.gmc.global.service.mapper.OpenAuthMapper;
import com.ibeeking.gmc.global.service.mapper.OpenConfigMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.redis.utils.RedisUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName OpenConfigServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-06-16 16:06
 **/
@Service
public class OpenAuthServiceImpl implements IOpenAuthService {

    private static final String AUTH_LOCK = "AUTH_LOCK:";

    @Resource
    private OpenConfigMapper openConfigMapper;
    @Resource
    private OpenAuthMapper openAuthMapper;
    @Resource
    private OpenAuthFuncMapper openAuthFuncMapper;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private IOpenConfigService wxOpenConfigServiceImpl;
    @Resource
    private IDictValueService dictValueServiceImpl;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean modifyAuth(OpenAuthParam param) {
        Integer configType = param.getConfigType();
        Integer configSubType = param.getConfigSubType();
        Integer serviceAppType = param.getAppType();

        if (configType == OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_OPEN.getCode()) {
            if(configSubType == WxOpenAppTypeEnum.WECHAT_OPEN_APP_TYPE_TP.getCode()) {
                if (serviceAppType == WxOpenServiceTypeConstant.APPID_TYPE_MINI) {
                    return saveAndUpdateOpenAuthOfWxMa(param);
                } else if (serviceAppType == WxOpenServiceTypeConstant.APPID_TYPE_MP) {
                    return saveAndUpdateOpenAuthOfWxMp(param);
                }
            }
        }else if (configType == OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode()) {
            if(configSubType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_TP.getCode()) {
                return saveAndUpdateOpenAuthOfWxCpTp(param);
            }else if(configSubType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_DK.getCode()) {
                return saveAndUpdateOpenAuthOfWxCpDk(param);
            }else if(configSubType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_SB.getCode()) {
                return saveAndUpdateOpenAuthOfWxCpSb(param);
            }else if(configSubType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_CONTACT.getCode()) {
                return saveAndUpdateOpenAuthOfWxCpContact(param);
            }
        }else if (configType == OpenApiConfigEnum.OPEN_CONFIG_TYPE_DINGTALK_OPEN.getCode()) {
            if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_WEB.getCode()) {
                return saveAndUpdateOpenAuthOfAliDingTalkTp(param);
            }else if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_MA.getCode()) {
                return saveAndUpdateOpenAuthOfAliDingTalkTp(param);
            }else if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_CORP_APP_TYPE_WEB.getCode()) {
                return saveAndUpdateOpenAuthOfAliDingTalkSb(param);
            }else if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_CORP_APP_TYPE_MA.getCode()) {
                return saveAndUpdateOpenAuthOfAliDingTalkSb(param);
            }else if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_PERSON.getCode()) {
                return saveAndUpdateOpenAuthOfAliDingTalkTp(param);
            }
        }
        return true;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean modifyAuthTenantId(OpenAuthModifyTenantIdParam param) {
        List<OpenAuthDO> openAuthDOS = openAuthMapper.selectList(Wrappers.lambdaQuery(OpenAuthDO.class).eq(OpenAuthDO::getTenantId, param.getTenantId()).eq(OpenAuthDO::getPublishStatus, PublishStatusEnum.ENABLE.getValue()).eq(OpenAuthDO::getAppType, param.getType()));
        DataUtils.trueThrowException(Collections3.size(openAuthDOS) > 1 || (Collections3.size(openAuthDOS) == 1 && !Objects.equals(openAuthDOS.get(0).getAppId(), param.getAppId())), "当前商场只能绑定一个小程序或公众号");
        LambdaUpdateWrapper<OpenAuthDO> updateWrapper = Wrappers.lambdaUpdate(OpenAuthDO.class).eq(OpenAuthDO::getAppId, param.getAppId()).set(OpenAuthDO::getTenantId, param.getTenantId());
        return openAuthMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public Boolean cancelAuth(OpenAuthCancelParam param) {
        if (StringUtils.isBlank(param.getAppId()) || StringUtils.isBlank(param.getOpenAppId())) {
            return true;
        }
        //直接删除授权数据
        List<OpenAuthDO> openAuthDOS = openAuthMapper.selectList(Wrappers.lambdaQuery(OpenAuthDO.class)
                .eq(OpenAuthDO::getAppId, param.getAppId())
                .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
        );
        if(Collections3.isNotEmpty(openAuthDOS)){
            openAuthDOS.stream().forEach(s->{
                openAuthFuncMapper.delete(new LambdaUpdateWrapper<>(OpenAuthFuncDO.class).eq(OpenAuthFuncDO::getAuthId, s.getId()));
                openAuthMapper.deleteById(s.getId());
            });
            return true;
        }
        return true;
    }

    @Override
    public List<OpenAuthDTO> queryOpenAuthList(OpenAuthQuery query){
        List<OpenAuthDO> entityDOS = openAuthMapper.selectList(new LambdaQueryWrapper<>(OpenAuthDO.class)
                .eq(query.getId()!=null, OpenAuthDO::getId, query.getId())
                .eq(query.getOpenId()!=null, OpenAuthDO::getOpenId, query.getOpenId())
                .eq(query.getTenantId()!=null, OpenAuthDO::getTenantId, query.getTenantId())
                .eq(query.getConfigType()!=null, OpenAuthDO::getConfigType, query.getConfigType())
                .eq(query.getConfigSubType()!=null, OpenAuthDO::getConfigSubType, query.getConfigSubType())
                .eq(query.getAppType()!=null, OpenAuthDO::getAppType, query.getAppType())
                .eq(query.getAppSubType()!=null, OpenAuthDO::getAppSubType, query.getAppSubType())
                .eq(StringUtils.isNotEmpty(query.getOpenAppId()), OpenAuthDO::getOpenAppId, query.getOpenAppId())
                .eq(StringUtils.isNotEmpty(query.getAppId()), OpenAuthDO::getAppId, query.getAppId())
                .eq(StringUtils.isNotEmpty(query.getComponentAppId()), OpenAuthDO::getComponentAppId, query.getComponentAppId())
                .eq(OpenAuthDO::getPublishStatus, PublishStatusEnum.ENABLE.getValue())
        );
        List<OpenAuthDTO> entityDTOS = new ArrayList<OpenAuthDTO>();
        if(Collections3.isNotEmpty(entityDOS)){
            entityDTOS = entityDOS.stream().map(d->{
              OpenAuthDTO wxOpenAuthDTO = transferOpenAuthDTO(d);
              return wxOpenAuthDTO;
            }).collect(Collectors.toList());
        }
        return entityDTOS;
    }

    @Override
    public List<SimpleOpenAuthDTO> querySimpleOpenAuthList(OpenAuthQuery query){
        List<OpenAuthDO> entityDOS = openAuthMapper.selectList(new LambdaQueryWrapper<>(OpenAuthDO.class)
                .eq(query.getId()!=null, OpenAuthDO::getId, query.getId())
                .eq(query.getOpenId()!=null, OpenAuthDO::getOpenId, query.getOpenId())
                .eq(query.getTenantId()!=null, OpenAuthDO::getTenantId, query.getTenantId())
                .eq(query.getConfigType()!=null, OpenAuthDO::getConfigType, query.getConfigType())
                .eq(query.getConfigSubType()!=null, OpenAuthDO::getConfigSubType, query.getConfigSubType())
                .eq(query.getAppType()!=null, OpenAuthDO::getAppType, query.getAppType())
                .eq(query.getAppSubType()!=null, OpenAuthDO::getAppSubType, query.getAppSubType())
                .eq(StringUtils.isNotEmpty(query.getOpenAppId()), OpenAuthDO::getOpenAppId, query.getOpenAppId())
                .eq(StringUtils.isNotEmpty(query.getAppId()), OpenAuthDO::getAppId, query.getAppId())
                .eq(StringUtils.isNotEmpty(query.getComponentAppId()), OpenAuthDO::getComponentAppId, query.getComponentAppId())
                .eq(OpenAuthDO::getPublishStatus, PublishStatusEnum.ENABLE.getValue())
        );
        List<SimpleOpenAuthDTO> entityDTOS = new ArrayList<SimpleOpenAuthDTO>();
        if(Collections3.isNotEmpty(entityDOS)){
            entityDTOS = entityDOS.stream().map(d->{
                String extendJson = d.getExtendJson();
                SimpleOpenAuthDTO openAuthDTO = BeanUtil.convertBean(d, SimpleOpenAuthDTO.class);
                openAuthDTO.setHasOpenAuth(false);
                if(StringUtils.isNotEmpty(extendJson)){
                    Integer configType = d.getConfigType();
                    Integer configSubType = d.getConfigSubType();
                    Integer appType = d.getAppType();
                    OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
                    if(openAuthOfExtend!=null) {
                        if (configType == OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_OPEN.getCode() && configSubType == WxOpenAppTypeEnum.WECHAT_OPEN_APP_TYPE_TP.getCode()) {
                            if (appType == WxOpenServiceTypeConstant.APPID_TYPE_MINI) {
                                OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
                                openAuthDTO.setSignature(openAuthOfWxOpen.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfWxOpen.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfWxOpen.getNonceStr());
                                openAuthDTO.setHasOpenAuth(openAuthOfWxOpen.getAuthorizerAccessToken() != null);
                            } else if (appType == WxOpenServiceTypeConstant.APPID_TYPE_MP) {
                                OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
                                openAuthDTO.setSignature(openAuthOfWxOpen.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfWxOpen.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfWxOpen.getNonceStr());
                                openAuthDTO.setHasOpenAuth(openAuthOfWxOpen.getAuthorizerAccessToken() != null);
                            }
                        } else if (configType == OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode()) {
                            if (configSubType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_SB.getCode()) {
                                OpenAuthOfWxCpSb openAuthOfWxCpSb = openAuthOfExtend.getOpenAuthOfWxCpSb();
                                openAuthDTO.setSignature(openAuthOfWxCpSb.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfWxCpSb.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfWxCpSb.getNonceStr());
                                openAuthDTO.setHasOpenAuth(StringUtils.isNotEmpty(openAuthOfWxCpSb.getAccessToken()));
                            } else if (configSubType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_TP.getCode()) {
                                OpenAuthOfWxCpTp openAuthOfWxCpTp = openAuthOfExtend.getOpenAuthOfWxCpTp();
                                openAuthDTO.setSignature(openAuthOfWxCpTp.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfWxCpTp.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfWxCpTp.getNonceStr());
                                openAuthDTO.setHasOpenAuth(StringUtils.isNotEmpty(openAuthOfWxCpTp.getCorpAccessToken()));
                            } else if (configSubType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_DK.getCode()) {
                                OpenAuthOfWxCpDk openAuthOfWxCpDk = openAuthOfExtend.getOpenAuthOfWxCpDk();
                                openAuthDTO.setSignature(openAuthOfWxCpDk.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfWxCpDk.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfWxCpDk.getNonceStr());
                                openAuthDTO.setHasOpenAuth(StringUtils.isNotEmpty(openAuthOfWxCpDk.getSuiteAccessToken()));
                            }
                        }else if (configType == OpenApiConfigEnum.OPEN_CONFIG_TYPE_DINGTALK_OPEN.getCode()) {
                            if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_WEB.getCode()) {
                                OpenAuthOfAliDingTalkTp openAuthOfAliDingTalkTp = openAuthOfExtend.getOpenAuthOfAliDingTalkTp();
                                openAuthDTO.setSignature(openAuthOfAliDingTalkTp.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfAliDingTalkTp.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfAliDingTalkTp.getNonceStr());
                                openAuthDTO.setHasOpenAuth(StringUtils.isNotEmpty(openAuthOfAliDingTalkTp.getAccessToken()));
                            }else if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_MA.getCode()) {
                                OpenAuthOfAliDingTalkTp openAuthOfAliDingTalkTp = openAuthOfExtend.getOpenAuthOfAliDingTalkTp();
                                openAuthDTO.setSignature(openAuthOfAliDingTalkTp.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfAliDingTalkTp.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfAliDingTalkTp.getNonceStr());
                                openAuthDTO.setHasOpenAuth(StringUtils.isNotEmpty(openAuthOfAliDingTalkTp.getAccessToken()));
                            }else if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_CORP_APP_TYPE_WEB.getCode()) {
                                OpenAuthOfAliDingTalkSb openAuthOfAliDingTalkSb = openAuthOfExtend.getOpenAuthOfAliDingTalkSb();
                                openAuthDTO.setSignature(openAuthOfAliDingTalkSb.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfAliDingTalkSb.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfAliDingTalkSb.getNonceStr());
                                openAuthDTO.setHasOpenAuth(StringUtils.isNotEmpty(openAuthOfAliDingTalkSb.getAccessToken()));
                            }else if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_CORP_APP_TYPE_MA.getCode()) {
                                OpenAuthOfAliDingTalkSb openAuthOfAliDingTalkSb = openAuthOfExtend.getOpenAuthOfAliDingTalkSb();
                                openAuthDTO.setSignature(openAuthOfAliDingTalkSb.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfAliDingTalkSb.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfAliDingTalkSb.getNonceStr());
                                openAuthDTO.setHasOpenAuth(StringUtils.isNotEmpty(openAuthOfAliDingTalkSb.getAccessToken()));
                            }else if(configSubType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_PERSON.getCode()) {
                                OpenAuthOfAliDingTalkTp openAuthOfAliDingTalkTp = openAuthOfExtend.getOpenAuthOfAliDingTalkTp();
                                openAuthDTO.setSignature(openAuthOfAliDingTalkTp.getSignature());
                                openAuthDTO.setTimestamp(openAuthOfAliDingTalkTp.getTimestamp());
                                openAuthDTO.setNonceStr(openAuthOfAliDingTalkTp.getNonceStr());
                                openAuthDTO.setHasOpenAuth(StringUtils.isNotEmpty(openAuthOfAliDingTalkTp.getAccessToken()));
                            }
                        }
                    }
                }
                return openAuthDTO;
            }).collect(Collectors.toList());
        }
        return entityDTOS;
    }

    @Override
    public OpenAuthDTO queryOpenAuthOne(OpenAuthQuery query){
        List<OpenAuthDTO> openAuthDTOS = this.getBean().queryOpenAuthList(query);
        if(Collections3.isNotEmpty(openAuthDTOS)) {
            return openAuthDTOS.get(0);
        }else {
            return null;
        }
    }

    @Override
    public SimpleOpenAuthDTO querySimpleOpenAuthOne(OpenAuthQuery query){
        List<SimpleOpenAuthDTO> openAuthDTOS = this.getBean().querySimpleOpenAuthList(query);
        if(Collections3.isNotEmpty(openAuthDTOS)) {
            return openAuthDTOS.get(0);
        }else {
            return null;
        }
    }

    /**
     * 查询授权基本信息
     *
     * @param appId 被授权公众号/小程序的appId
     * @return
     */
    @Override
    public List<OpenAuthOfWxOpenFunc> queryOpenAuthFuncByAppId(String appId) {
        List<OpenAuthFuncDO> openAuthFuncDOS = openAuthFuncMapper.selectList(new LambdaQueryWrapper<>(OpenAuthFuncDO.class)
                .eq(OpenAuthFuncDO::getAppId, appId)
                .orderByAsc(OpenAuthFuncDO::getFuncscopeCategory)
        );
        if (Collections3.isEmpty(openAuthFuncDOS)) {
            return Collections3.emptyList();
        }
        //从字典获取微信授权信息中文描述
        List<DictDTO> dictDTOS = dictValueServiceImpl.queryDictByType(DictTypeEnum.WX_OPEN_AUTH_FUNC_DESC.getValue());
        Map<Integer, DictDTO> dictDTOMap = dictDTOS.stream().collect(Collectors.toMap(x -> Integer.valueOf(x.getValue()), x -> x, (o, n) -> n));
        List<OpenAuthOfWxOpenFunc> wxOpenAuthFuncDTOS = JsonUtils.jsonToList(openAuthFuncDOS, OpenAuthOfWxOpenFunc.class);
        for (OpenAuthOfWxOpenFunc wxOpenAuthFuncDTO : wxOpenAuthFuncDTOS) {
            DictDTO dictDTO = dictDTOMap.get(wxOpenAuthFuncDTO.getFuncscopeCategory());
            if (dictDTO != null) {
                wxOpenAuthFuncDTO.setFuncscopeCategoryName(dictDTO.getLabel());
                wxOpenAuthFuncDTO.setFuncscopeCategoryRemark(dictDTO.getRemark());
            }
        }
        return wxOpenAuthFuncDTOS;
    }
    /**
     * 查询授权基本信息
     *
     * @param authId 被授权公众号/小程序的appId
     * @return
     */
    @Override
    public List<OpenAuthOfWxOpenFunc> queryOpenAuthFuncByAuthId(Long authId) {
        List<OpenAuthFuncDO> openAuthFuncDOS = openAuthFuncMapper.queryByAuthId(authId);
        if (Collections3.isEmpty(openAuthFuncDOS)) {
            return Collections3.emptyList();
        }
        //从字典获取微信授权信息中文描述
        List<DictDTO> dictDTOS = dictValueServiceImpl.queryDictByType(DictTypeEnum.WX_OPEN_AUTH_FUNC_DESC.getValue());
        Map<Integer, DictDTO> dictDTOMap = dictDTOS.stream().collect(Collectors.toMap(x -> Integer.valueOf(x.getValue()), x -> x, (o, n) -> n));
        List<OpenAuthOfWxOpenFunc> wxOpenAuthFuncDTOS = JsonUtils.jsonToList(openAuthFuncDOS, OpenAuthOfWxOpenFunc.class);
        for (OpenAuthOfWxOpenFunc wxOpenAuthFuncDTO : wxOpenAuthFuncDTOS) {
            DictDTO dictDTO = dictDTOMap.get(wxOpenAuthFuncDTO.getFuncscopeCategory());
            if (dictDTO != null) {
                wxOpenAuthFuncDTO.setFuncscopeCategoryName(dictDTO.getLabel());
                wxOpenAuthFuncDTO.setFuncscopeCategoryRemark(dictDTO.getRemark());
            }
        }
        return wxOpenAuthFuncDTOS;
    }

    @Override
    public HasOpenAuthVO queryHasOpenAuth(Integer tenantId) {
        List<OpenAuthDO> openAuthDOList = openAuthMapper.selectList(new LambdaQueryWrapper<>(OpenAuthDO.class)
                .eq(OpenAuthDO::getTenantId, tenantId)
                .eq(OpenAuthDO::getPublishStatus, PublishStatusEnum.ENABLE.getValue())
        );
        Set<Integer> wxOpenAuthSet = openAuthDOList.stream().map(OpenAuthDO::getAppType).collect(Collectors.toSet());
        HasOpenAuthVO hasOpenAuthVO = new HasOpenAuthVO();
        hasOpenAuthVO.setHasWxOpenAppletAuth(wxOpenAuthSet.contains(1));
        hasOpenAuthVO.setHasWxOpenMpAuth(wxOpenAuthSet.contains(2));
        return hasOpenAuthVO;
    }

    private Boolean saveAndUpdateOpenAuthOfWxMp(OpenAuthParam param) {
        if(param!=null){
            Long openAuthId = null;
            Integer configType = param.getConfigType();
            Integer configSubType = param.getConfigSubType();
            Integer serviceAppType = param.getAppType();
            if (redisUtils.tryLock(AUTH_LOCK.concat("wxmp:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getAppId()))) {
                //查询是否已经存在
                LogUtils.info("查询缓存中是否存在授权参数:{}", JSON.toJSONString(param));
                try {
                    OpenAuthDO openAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                            .eq(OpenAuthDO::getAppId, param.getAppId())
                            .eq(OpenAuthDO::getOpenAppId, param.getOpenAppId())
                            .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
                    );
                    if (null != openAuthDO) {
                        openAuthId = openAuthDO.getId();
                    }
                } finally {
                    redisUtils.unlock(AUTH_LOCK.concat("wxmp:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getAppId()));
                }
            }
            OpenAuthDO openAuthDO = BeanUtil.convertBean(param, OpenAuthDO.class);
            OpenConfigQuery configQuery = new OpenConfigQuery();
            configQuery.setType(configType);
            configQuery.setSubType(configSubType);
            OpenConfigDTO openConfigDTO = wxOpenConfigServiceImpl.queryOpenConfigOne(configQuery);
            if (null == openConfigDTO) {
                throw new BusinessException("未配置开放平台参数");
            }
            if(StringUtils.isBlank(openAuthDO.getOpenAppId()) || (openAuthDO.getOpenId() == null)) {
                openAuthDO.setOpenId(openConfigDTO.getId());
                openAuthDO.setOpenAppId(openConfigDTO.getOpenAppId());
                openAuthDO.setComponentAppId(openConfigDTO.getComponentAppId());
            }
            OpenAuthOfExtend openAuthOfExtend = param.getOpenAuthOfExtend();
            if (null != openAuthOfExtend) {
                openAuthDO.setExtendJson(JsonUtils.toJsonStr(openAuthOfExtend));
            }
            openAuthDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            openAuthDO.setTenantId(param.getTenantId());
            if (null != openAuthId) {
                openAuthDO.setId(openAuthId);
                openAuthDO.setModifyBy(UserUtils.getUserId());
                openAuthDO.setModifyTime(LocalDateTime.now());
                LogUtils.info("更新开放平台授权参数:{}", JSON.toJSONString(openAuthDO));

                openAuthMapper.updateById(openAuthDO);
                if(null != openAuthOfExtend) {
                    OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
                    if(openAuthOfWxOpen!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxOpen openConfigOfWxOpen = new OpenConfigOfWxOpen();
                        openConfigOfWxOpen.setComponentVerifyTicket(openAuthOfWxOpen.getComponentVerifyTicket());
                        openConfigOfWxOpen.setComponentAccessToken(openAuthOfWxOpen.getComponentAccessToken());
                        openConfigOfExtend.setOpenConfigOfWxOpen(openConfigOfWxOpen);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            } else {
                // 确保一个租户下只有一个授权小程序/公众号
                OpenAuthDO insOpenAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                        .eq(OpenAuthDO::getTenantId, param.getTenantId())
                        .eq(OpenAuthDO::getConfigType, param.getConfigType())
                        .eq(OpenAuthDO::getConfigSubType, param.getConfigSubType())
                        .eq(OpenAuthDO::getAppType, param.getAppType()));
                if(null != insOpenAuthDO){
                    throw new BusinessException("一个租户只能授权一个公众号！");
                }
                openAuthDO.setCreateBy(UserUtils.getUserId());
                openAuthDO.setCreateTime(LocalDateTime.now());
                openAuthDO.setModifyTime(LocalDateTime.now());
                openAuthDO.setId(IdGenerator.getId());
                LogUtils.info("插入开放平台授权参数:{}", JSON.toJSONString(openAuthDO));
                openAuthMapper.insert(openAuthDO);
                if(openAuthOfExtend != null) {
                    OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
                    if(openAuthOfWxOpen!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxOpen openConfigOfWxOpen = new OpenConfigOfWxOpen();
                        openConfigOfWxOpen.setComponentVerifyTicket(openAuthOfWxOpen.getComponentVerifyTicket());
                        openConfigOfWxOpen.setComponentAccessToken(openAuthOfWxOpen.getComponentAccessToken());
                        openConfigOfExtend.setOpenConfigOfWxOpen(openConfigOfWxOpen);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            }
            return true;
        }
        return false;
    }

    private Boolean saveAndUpdateOpenAuthOfWxMa(OpenAuthParam param) {
        if(param!=null){
            Long openAuthId = null;
            Integer configType = param.getConfigType();
            Integer configSubType = param.getConfigSubType();
            Integer serviceAppType = param.getAppType();
            if (redisUtils.tryLock(AUTH_LOCK.concat("wxma:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getAppId()))) {
                //查询是否已经存在
                LogUtils.info("查询缓存中是否存在授权参数:{}", JSON.toJSONString(param));
                try {
                    OpenAuthDO openAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                            .eq(OpenAuthDO::getAppId, param.getAppId())
                            .eq(OpenAuthDO::getOpenAppId, param.getOpenAppId())
                            .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
                    );
                    if (null != openAuthDO) {
                        openAuthId = openAuthDO.getId();
                    }
                } finally {
                    redisUtils.unlock(AUTH_LOCK.concat("wxma:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getAppId()));
                }
            }
            OpenAuthDO openAuthDO = BeanUtil.convertBean(param, OpenAuthDO.class);
            OpenConfigQuery configQuery = new OpenConfigQuery();
            configQuery.setType(configType);
            configQuery.setSubType(configSubType);
            OpenConfigDTO openConfigDTO = wxOpenConfigServiceImpl.queryOpenConfigOne(configQuery);
            if (null == openConfigDTO) {
                throw new BusinessException("未配置开放平台参数");
            }
            if(StringUtils.isBlank(openAuthDO.getOpenAppId()) || (openAuthDO.getOpenId() == null)) {
                openAuthDO.setOpenId(openConfigDTO.getId());
                openAuthDO.setOpenAppId(openConfigDTO.getOpenAppId());
                openAuthDO.setComponentAppId(openConfigDTO.getComponentAppId());
            }
            OpenAuthOfExtend openAuthOfExtend = param.getOpenAuthOfExtend();
            if (null != openAuthOfExtend) {
                openAuthDO.setExtendJson(JsonUtils.toJsonStr(openAuthOfExtend));
            }
            openAuthDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            openAuthDO.setTenantId(param.getTenantId());
            if (null != openAuthId) {
                openAuthDO.setId(openAuthId);
                openAuthDO.setModifyBy(UserUtils.getUserId());
                openAuthDO.setModifyTime(LocalDateTime.now());
                LogUtils.info("更新开放平台授权参数:{}", JSON.toJSONString(openAuthDO));

                openAuthMapper.updateById(openAuthDO);
                if(null != openAuthOfExtend) {
                    OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
                    if(openAuthOfWxOpen!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxOpen openConfigOfWxOpen = new OpenConfigOfWxOpen();
                        openConfigOfWxOpen.setComponentVerifyTicket(openAuthOfWxOpen.getComponentVerifyTicket());
                        openConfigOfWxOpen.setComponentAccessToken(openAuthOfWxOpen.getComponentAccessToken());
                        openConfigOfExtend.setOpenConfigOfWxOpen(openConfigOfWxOpen);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            } else {
                // 确保一个租户下只有一个授权小程序/公众号
                OpenAuthDO insOpenAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                        .eq(OpenAuthDO::getTenantId, param.getTenantId())
                        .eq(OpenAuthDO::getConfigType, param.getConfigType())
                        .eq(OpenAuthDO::getConfigSubType, param.getConfigSubType())
                        .eq(OpenAuthDO::getAppType, param.getAppType()));
                if(null != insOpenAuthDO){
                    throw new BusinessException("一个租户只能授权一个小程序！");
                }
                openAuthDO.setCreateBy(UserUtils.getUserId());
                openAuthDO.setCreateTime(LocalDateTime.now());
                openAuthDO.setModifyTime(LocalDateTime.now());
                openAuthDO.setId(IdGenerator.getId());
                LogUtils.info("插入开放平台授权参数:{}", JSON.toJSONString(openAuthDO));
                openAuthMapper.insert(openAuthDO);
                if(openAuthOfExtend != null) {
                    OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
                    if(openAuthOfWxOpen!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxOpen openConfigOfWxOpen = new OpenConfigOfWxOpen();
                        openConfigOfWxOpen.setComponentVerifyTicket(openAuthOfWxOpen.getComponentVerifyTicket());
                        openConfigOfWxOpen.setComponentAccessToken(openAuthOfWxOpen.getComponentAccessToken());
                        openConfigOfExtend.setOpenConfigOfWxOpen(openConfigOfWxOpen);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            }
            return true;
        }
        return false;
    }

    private Boolean saveAndUpdateOpenAuthOfWxCpTp(OpenAuthParam param) {
        if(param!=null){
            Long openAuthId = null;
            Integer configType = param.getConfigType();
            Integer configSubType = param.getConfigSubType();
            Integer serviceAppType = param.getAppType();
            if (redisUtils.tryLock(AUTH_LOCK.concat("wx:cp:tp:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()))) {
                //查询是否已经存在
                LogUtils.info("查询缓存中是否存在授权参数:{}", JSON.toJSONString(param));
                try {
                    OpenAuthDO openAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                            .eq(OpenAuthDO::getTenantId, param.getTenantId())
                            .eq(OpenAuthDO::getOpenAppId, param.getOpenAppId())
                            .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
                    );
                    if (null != openAuthDO) {
                        openAuthId = openAuthDO.getId();
                    }
                } finally {
                    redisUtils.unlock(AUTH_LOCK.concat("wx:cp:tp:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()));
                }
            }
            OpenAuthDO openAuthDO = BeanUtil.convertBean(param, OpenAuthDO.class);
            OpenConfigQuery configQuery = new OpenConfigQuery();
            configQuery.setType(configType);
            configQuery.setSubType(configSubType);
            OpenConfigDTO openConfigDTO = wxOpenConfigServiceImpl.queryOpenConfigOne(configQuery);
            if (null == openConfigDTO) {
                throw new BusinessException("未配置开放平台参数");
            }
            if(StringUtils.isBlank(openAuthDO.getOpenAppId()) || (openAuthDO.getOpenId() == null)) {
                openAuthDO.setOpenId(openConfigDTO.getId());
                openAuthDO.setOpenAppId(openConfigDTO.getOpenAppId());
                openAuthDO.setComponentAppId(openConfigDTO.getComponentAppId());
            }
            OpenAuthOfExtend openAuthOfExtend = param.getOpenAuthOfExtend();
            if (null != openAuthOfExtend) {
                openAuthDO.setExtendJson(JsonUtils.toJsonStr(openAuthOfExtend));
            }
            openAuthDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            openAuthDO.setTenantId(param.getTenantId());
            if (null != openAuthId) {
                openAuthDO.setId(openAuthId);
                openAuthDO.setModifyBy(UserUtils.getUserId());
                openAuthDO.setModifyTime(LocalDateTime.now());
                LogUtils.info("更新企微三方应用授权参数:{}", JSON.toJSONString(openAuthDO));

                openAuthMapper.updateById(openAuthDO);
                if(null != openAuthOfExtend) {
                    OpenAuthOfWxCpTp openAuthOfWxCpTp = openAuthOfExtend.getOpenAuthOfWxCpTp();
                    if(openAuthOfWxCpTp!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxCp openConfigOfWxCp = new OpenConfigOfWxCp();
                        openConfigOfWxCp.setComponentVerifyTicket(openAuthOfWxCpTp.getSuiteTicket());
                        openConfigOfWxCp.setComponentAccessToken(openAuthOfWxCpTp.getSuiteAccessToken());
                        openConfigOfWxCp.setComponentAccessTokenExpireIn(openAuthOfWxCpTp.getSuiteAccessExpirein());
                        openConfigOfWxCp.setComponentAccessTokenExpireTimes(openAuthOfWxCpTp.getSuiteAccessExpireTime());
                        openConfigOfExtend.setOpenConfigOfWxCp(openConfigOfWxCp);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            } else {
                // 确保一个租户下只有一个授权小程序/公众号
                OpenAuthDO insOpenAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                        .eq(OpenAuthDO::getTenantId, param.getTenantId())
                        .eq(OpenAuthDO::getConfigType, param.getConfigType())
                        .eq(OpenAuthDO::getConfigSubType, param.getConfigSubType())
                        .eq(OpenAuthDO::getAppType, param.getAppType()));
                if(null != insOpenAuthDO){
                    throw new BusinessException("一个租户只能安装一个三方企微助手！");
                }
                openAuthDO.setCreateBy(UserUtils.getUserId());
                openAuthDO.setCreateTime(LocalDateTime.now());
                openAuthDO.setModifyTime(LocalDateTime.now());
                openAuthDO.setId(IdGenerator.getId());
                LogUtils.info("插入开放平台授权参数:{}", JSON.toJSONString(openAuthDO));
                openAuthMapper.insert(openAuthDO);
                if(openAuthOfExtend != null) {
                    OpenAuthOfWxCpTp openAuthOfWxCpTp = openAuthOfExtend.getOpenAuthOfWxCpTp();
                    if(openAuthOfWxCpTp!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxCp openConfigOfWxCp = new OpenConfigOfWxCp();
                        openConfigOfWxCp.setComponentVerifyTicket(openAuthOfWxCpTp.getSuiteTicket());
                        openConfigOfWxCp.setComponentAccessToken(openAuthOfWxCpTp.getSuiteAccessToken());
                        openConfigOfWxCp.setComponentAccessTokenExpireIn(openAuthOfWxCpTp.getSuiteAccessExpirein());
                        openConfigOfWxCp.setComponentAccessTokenExpireTimes(openAuthOfWxCpTp.getSuiteAccessExpireTime());
                        openConfigOfExtend.setOpenConfigOfWxCp(openConfigOfWxCp);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            }
            return true;
        }
        return false;
    }

    private Boolean saveAndUpdateOpenAuthOfWxCpDk(OpenAuthParam param) {
        if(param!=null){
            Long openAuthId = null;
            Integer configType = param.getConfigType();
            Integer configSubType = param.getConfigSubType();
            Integer serviceAppType = param.getAppType();
            if (redisUtils.tryLock(AUTH_LOCK.concat("wx:cp:dk:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()))) {
                //查询是否已经存在
                LogUtils.info("查询缓存中是否存在授权参数:{}", JSON.toJSONString(param));
                try {
                    OpenAuthDO openAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                            .eq(OpenAuthDO::getTenantId, param.getTenantId())
                            .eq(OpenAuthDO::getOpenAppId, param.getOpenAppId())
                            .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
                    );
                    if (null != openAuthDO) {
                        openAuthId = openAuthDO.getId();
                    }
                } finally {
                    redisUtils.unlock(AUTH_LOCK.concat("wx:cp:dk:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()));
                }
            }
            OpenAuthDO openAuthDO = BeanUtil.convertBean(param, OpenAuthDO.class);
            OpenConfigQuery configQuery = new OpenConfigQuery();
            configQuery.setType(configType);
            configQuery.setSubType(configSubType);
            OpenConfigDTO openConfigDTO = wxOpenConfigServiceImpl.queryOpenConfigOne(configQuery);
            if (null == openConfigDTO) {
                throw new BusinessException("未配置开放平台参数");
            }
            if(StringUtils.isBlank(openAuthDO.getOpenAppId()) || (openAuthDO.getOpenId() == null)) {
                openAuthDO.setOpenId(openConfigDTO.getId());
                openAuthDO.setOpenAppId(openConfigDTO.getOpenAppId());
                openAuthDO.setComponentAppId(openConfigDTO.getComponentAppId());
            }
            OpenAuthOfExtend openAuthOfExtend = param.getOpenAuthOfExtend();
            if (null != openAuthOfExtend) {
                openAuthDO.setExtendJson(JsonUtils.toJsonStr(openAuthOfExtend));
            }
            openAuthDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            openAuthDO.setTenantId(param.getTenantId());
            if (null != openAuthId) {
                openAuthDO.setId(openAuthId);
                openAuthDO.setModifyBy(UserUtils.getUserId());
                openAuthDO.setModifyTime(LocalDateTime.now());
                LogUtils.info("更新企微三方应用授权参数:{}", JSON.toJSONString(openAuthDO));

                openAuthMapper.updateById(openAuthDO);
                if(null != openAuthOfExtend) {
                    OpenAuthOfWxCpDk openAuthOfWxCpDk = openAuthOfExtend.getOpenAuthOfWxCpDk();
                    if(openAuthOfWxCpDk!=null) {
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        if(openConfigDO!=null) {
                            String configExtendjson = openConfigDO.getExtendJson();
                            OpenConfigOfExtend openConfigOfExtend = StringUtils.isNotEmpty(configExtendjson) ? JsonUtils.fromJson(configExtendjson, OpenConfigOfExtend.class) : new OpenConfigOfExtend();
                            OpenConfigOfWxCp openConfigOfWxCp = openConfigOfExtend.getOpenConfigOfWxCp();
                            if(openConfigOfWxCp == null) openConfigOfWxCp = new OpenConfigOfWxCp();
                            openConfigOfWxCp.setComponentVerifyTicket(openAuthOfWxCpDk.getSuiteTicket());
                            openConfigOfWxCp.setComponentAccessToken(openAuthOfWxCpDk.getSuiteAccessToken());
                            openConfigOfWxCp.setComponentAccessTokenExpireIn(openAuthOfWxCpDk.getSuiteAccessExpirein());
                            openConfigOfWxCp.setComponentAccessTokenExpireTimes(openAuthOfWxCpDk.getSuiteAccessExpireTime());
                            openConfigOfExtend.setOpenConfigOfWxCp(openConfigOfWxCp);

                            openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                            openConfigMapper.updateById(openConfigDO);
                        }
                    }
                }
            } else {
                // 确保一个租户下只有一个授权小程序/公众号
                OpenAuthDO insOpenAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                        .eq(OpenAuthDO::getTenantId, param.getTenantId())
                        .eq(OpenAuthDO::getConfigType, param.getConfigType())
                        .eq(OpenAuthDO::getConfigSubType, param.getConfigSubType())
                        .eq(OpenAuthDO::getAppType, param.getAppType()));
                if(null != insOpenAuthDO){
                    throw new BusinessException("一个租户只能授权一个自建应用！");
                }
                openAuthDO.setCreateBy(UserUtils.getUserId());
                openAuthDO.setCreateTime(LocalDateTime.now());
                openAuthDO.setModifyTime(LocalDateTime.now());
                openAuthDO.setId(IdGenerator.getId());
                LogUtils.info("插入开放平台授权参数:{}", JSON.toJSONString(openAuthDO));
                openAuthMapper.insert(openAuthDO);
                if(openAuthOfExtend != null) {
                    OpenAuthOfWxCpDk openAuthOfWxCpDk = openAuthOfExtend.getOpenAuthOfWxCpDk();
                    if(openAuthOfWxCpDk!=null) {
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        if (openConfigDO != null) {
                            String configExtendjson = openConfigDO.getExtendJson();
                            OpenConfigOfExtend openConfigOfExtend = StringUtils.isNotEmpty(configExtendjson) ? JsonUtils.fromJson(configExtendjson, OpenConfigOfExtend.class) : new OpenConfigOfExtend();
                            OpenConfigOfWxCp openConfigOfWxCp = openConfigOfExtend.getOpenConfigOfWxCp();
                            if (openConfigOfWxCp == null) openConfigOfWxCp = new OpenConfigOfWxCp();
                            openConfigOfWxCp.setComponentVerifyTicket(openAuthOfWxCpDk.getSuiteTicket());
                            openConfigOfWxCp.setComponentAccessToken(openAuthOfWxCpDk.getSuiteAccessToken());
                            openConfigOfWxCp.setComponentAccessTokenExpireIn(openAuthOfWxCpDk.getSuiteAccessExpirein());
                            openConfigOfWxCp.setComponentAccessTokenExpireTimes(openAuthOfWxCpDk.getSuiteAccessExpireTime());
                            openConfigOfExtend.setOpenConfigOfWxCp(openConfigOfWxCp);
                            openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                            openConfigMapper.updateById(openConfigDO);
                        }
                    }
                }
            }
            return true;
        }
        return false;
    }

    private Boolean saveAndUpdateOpenAuthOfWxCpSb(OpenAuthParam param) {
        if(param!=null){
            Long openAuthId = null;
            Integer configType = param.getConfigType();
            Integer configSubType = param.getConfigSubType();
            Integer serviceAppType = param.getAppType();
            if (redisUtils.tryLock(AUTH_LOCK.concat("wx:cp:sb:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()))) {
                //查询是否已经存在
                LogUtils.info("查询缓存中是否存在授权参数:{}", JSON.toJSONString(param));
                try {
                    OpenAuthDO openAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                            .eq(OpenAuthDO::getTenantId, param.getTenantId())
                            .eq(OpenAuthDO::getOpenAppId, param.getOpenAppId())
                            .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
                    );
                    if (null != openAuthDO) {
                        openAuthId = openAuthDO.getId();
                    }
                } finally {
                    redisUtils.unlock(AUTH_LOCK.concat("wx:cp:sb:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()));
                }
            }
            OpenAuthDO openAuthDO = BeanUtil.convertBean(param, OpenAuthDO.class);
            OpenConfigQuery configQuery = new OpenConfigQuery();
            configQuery.setType(configType);
            configQuery.setSubType(configSubType);
            OpenConfigDTO openConfigDTO = wxOpenConfigServiceImpl.queryOpenConfigOne(configQuery);
            if (null == openConfigDTO) {
                throw new BusinessException("未配置开放平台参数");
            }
            if(StringUtils.isBlank(openAuthDO.getOpenAppId()) || (openAuthDO.getOpenId() == null)) {
                openAuthDO.setOpenId(openConfigDTO.getId());
                openAuthDO.setOpenAppId(openConfigDTO.getOpenAppId());
                openAuthDO.setComponentAppId(openConfigDTO.getComponentAppId());
            }
            OpenAuthOfExtend openAuthOfExtend = param.getOpenAuthOfExtend();
            if (null != openAuthOfExtend) {
                openAuthDO.setExtendJson(JsonUtils.toJsonStr(openAuthOfExtend));
            }
            openAuthDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            openAuthDO.setTenantId(param.getTenantId());
            if (null != openAuthId) {
                openAuthDO.setId(openAuthId);
                openAuthDO.setModifyBy(UserUtils.getUserId());
                openAuthDO.setModifyTime(LocalDateTime.now());
                LogUtils.info("更新企微三方应用授权参数:{}", JSON.toJSONString(openAuthDO));

                openAuthMapper.updateById(openAuthDO);
                if(null != openAuthOfExtend) {
                    OpenAuthOfWxCpSb openAuthOfWxCpSb = openAuthOfExtend.getOpenAuthOfWxCpSb();
                    if(openAuthOfWxCpSb!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxCp openConfigOfWxCp = new OpenConfigOfWxCp();
                        openConfigOfWxCp.setComponentAccessToken(openAuthOfWxCpSb.getAccessToken());
                        openConfigOfWxCp.setComponentAccessTokenExpireIn(openAuthOfWxCpSb.getAccessTokenExpiresIn());
                        openConfigOfWxCp.setComponentAccessTokenExpireTimes(openAuthOfWxCpSb.getAccessTokenExpiresTime());
                        openConfigOfExtend.setOpenConfigOfWxCp(openConfigOfWxCp);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            } else {
                // 确保一个租户下只有一个授权小程序/公众号
                OpenAuthDO insOpenAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                        .eq(OpenAuthDO::getTenantId, param.getTenantId())
                        .eq(OpenAuthDO::getConfigType, param.getConfigType())
                        .eq(OpenAuthDO::getConfigSubType, param.getConfigSubType())
                        .eq(OpenAuthDO::getAppType, param.getAppType()));
                if(null != insOpenAuthDO){
                    throw new BusinessException("一个租户只能授权一个小程序/公众号！");
                }
                openAuthDO.setCreateBy(UserUtils.getUserId());
                openAuthDO.setCreateTime(LocalDateTime.now());
                openAuthDO.setModifyTime(LocalDateTime.now());
                openAuthDO.setId(IdGenerator.getId());
                LogUtils.info("插入开放平台授权参数:{}", JSON.toJSONString(openAuthDO));
                openAuthMapper.insert(openAuthDO);
                if(openAuthOfExtend != null) {
                    OpenAuthOfWxCpSb openAuthOfWxCpSb = openAuthOfExtend.getOpenAuthOfWxCpSb();
                    if(openAuthOfWxCpSb!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxCp openConfigOfWxCp = new OpenConfigOfWxCp();
                        openConfigOfWxCp.setComponentAccessToken(openAuthOfWxCpSb.getAccessToken());
                        openConfigOfWxCp.setComponentAccessTokenExpireIn(openAuthOfWxCpSb.getAccessTokenExpiresIn());
                        openConfigOfWxCp.setComponentAccessTokenExpireTimes(openAuthOfWxCpSb.getAccessTokenExpiresTime());
                        openConfigOfExtend.setOpenConfigOfWxCp(openConfigOfWxCp);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            }
            return true;
        }
        return false;
    }

    private Boolean saveAndUpdateOpenAuthOfWxCpContact(OpenAuthParam param) {
        if(param!=null){
            Long openAuthId = null;
            Integer configType = param.getConfigType();
            Integer configSubType = param.getConfigSubType();
            Integer serviceAppType = param.getAppType();
            if (redisUtils.tryLock(AUTH_LOCK.concat("wx:cp:contact:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()))) {
                //查询是否已经存在
                LogUtils.info("查询缓存中是否存在授权参数:{}", JSON.toJSONString(param));
                try {
                    OpenAuthDO openAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                            .eq(OpenAuthDO::getTenantId, param.getTenantId())
                            .eq(OpenAuthDO::getOpenAppId, param.getOpenAppId())
                            .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
                    );
                    if (null != openAuthDO) {
                        openAuthId = openAuthDO.getId();
                    }
                } finally {
                    redisUtils.unlock(AUTH_LOCK.concat("wx:cp:contact:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()));
                }
            }
            OpenAuthDO openAuthDO = BeanUtil.convertBean(param, OpenAuthDO.class);
            OpenConfigQuery configQuery = new OpenConfigQuery();
            configQuery.setType(configType);
            configQuery.setSubType(configSubType);
            OpenConfigDTO openConfigDTO = wxOpenConfigServiceImpl.queryOpenConfigOne(configQuery);
            if (null == openConfigDTO) {
                throw new BusinessException("未配置开放平台参数");
            }
            if(StringUtils.isBlank(openAuthDO.getOpenAppId()) || (openAuthDO.getOpenId() == null)) {
                openAuthDO.setOpenId(openConfigDTO.getId());
                openAuthDO.setOpenAppId(openConfigDTO.getOpenAppId());
                openAuthDO.setComponentAppId(openConfigDTO.getComponentAppId());
            }
            OpenAuthOfExtend openAuthOfExtend = param.getOpenAuthOfExtend();
            if (null != openAuthOfExtend) {
                openAuthDO.setExtendJson(JsonUtils.toJsonStr(openAuthOfExtend));
            }
            openAuthDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            openAuthDO.setTenantId(param.getTenantId());
            if (null != openAuthId) {
                openAuthDO.setId(openAuthId);
                openAuthDO.setModifyBy(UserUtils.getUserId());
                openAuthDO.setModifyTime(LocalDateTime.now());
                LogUtils.info("更新企微三方应用授权参数:{}", JSON.toJSONString(openAuthDO));

                openAuthMapper.updateById(openAuthDO);
                if(null != openAuthOfExtend) {
                    OpenAuthOfWxCpContact openAuthOfWxCpContact = openAuthOfExtend.getOpenAuthOfWxCpContact();
                    if(openAuthOfWxCpContact!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxCp openConfigOfWxCp = new OpenConfigOfWxCp();
                        openConfigOfWxCp.setComponentVerifyTicket(openAuthOfWxCpContact.getSuiteTicket());
                        openConfigOfWxCp.setComponentAccessToken(openAuthOfWxCpContact.getSuiteAccessToken());
                        openConfigOfWxCp.setComponentAccessTokenExpireIn(openAuthOfWxCpContact.getSuiteAccessExpirein());
                        openConfigOfWxCp.setComponentAccessTokenExpireTimes(openAuthOfWxCpContact.getSuiteAccessExpireTime());
                        openConfigOfExtend.setOpenConfigOfWxCp(openConfigOfWxCp);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            } else {
                // 确保一个租户下只有一个授权小程序/公众号
                OpenAuthDO insOpenAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                        .eq(OpenAuthDO::getTenantId, param.getTenantId())
                        .eq(OpenAuthDO::getConfigType, param.getConfigType())
                        .eq(OpenAuthDO::getConfigSubType, param.getConfigSubType())
                        .eq(OpenAuthDO::getAppType, param.getAppType()));
                if(null != insOpenAuthDO){
                    throw new BusinessException("一个租户只能授权一个小程序/公众号！");
                }
                openAuthDO.setCreateBy(UserUtils.getUserId());
                openAuthDO.setCreateTime(LocalDateTime.now());
                openAuthDO.setModifyTime(LocalDateTime.now());
                openAuthDO.setId(IdGenerator.getId());
                LogUtils.info("插入开放平台授权参数:{}", JSON.toJSONString(openAuthDO));
                openAuthMapper.insert(openAuthDO);
                if(openAuthOfExtend != null) {
                    OpenAuthOfWxCpContact openAuthOfWxCpContact = openAuthOfExtend.getOpenAuthOfWxCpContact();
                    if(openAuthOfWxCpContact!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfWxCp openConfigOfWxCp = new OpenConfigOfWxCp();
                        openConfigOfWxCp.setComponentVerifyTicket(openAuthOfWxCpContact.getSuiteTicket());
                        openConfigOfWxCp.setComponentAccessToken(openAuthOfWxCpContact.getSuiteAccessToken());
                        openConfigOfWxCp.setComponentAccessTokenExpireIn(openAuthOfWxCpContact.getSuiteAccessExpirein());
                        openConfigOfWxCp.setComponentAccessTokenExpireTimes(openAuthOfWxCpContact.getSuiteAccessExpireTime());
                        openConfigOfExtend.setOpenConfigOfWxCp(openConfigOfWxCp);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            }
            return true;
        }
        return false;
    }

    private Boolean saveAndUpdateOpenAuthOfAliDingTalkTp(OpenAuthParam param) {
        if(param!=null){
            Long openAuthId = null;
            Integer configType = param.getConfigType();
            Integer configSubType = param.getConfigSubType();
            Integer serviceAppType = param.getAppType();
            if (redisUtils.tryLock(AUTH_LOCK.concat("dt:cp:tp:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()))) {
                //查询是否已经存在
                LogUtils.info("查询缓存中是否存在授权参数:{}", JSON.toJSONString(param));
                try {
                    OpenAuthDO openAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                            .eq(OpenAuthDO::getTenantId, param.getTenantId())
                            .eq(OpenAuthDO::getOpenAppId, param.getOpenAppId())
                            .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
                    );
                    if (null != openAuthDO) {
                        openAuthId = openAuthDO.getId();
                    }
                } finally {
                    redisUtils.unlock(AUTH_LOCK.concat("dt:cp:tp:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()));
                }
            }
            OpenAuthDO openAuthDO = BeanUtil.convertBean(param, OpenAuthDO.class);
            OpenConfigQuery configQuery = new OpenConfigQuery();
            configQuery.setType(configType);
            configQuery.setSubType(configSubType);
            OpenConfigDTO openConfigDTO = wxOpenConfigServiceImpl.queryOpenConfigOne(configQuery);
            if (null == openConfigDTO) {
                throw new BusinessException("未配置开放平台参数");
            }
            if(StringUtils.isBlank(openAuthDO.getOpenAppId()) || (openAuthDO.getOpenId() == null)) {
                openAuthDO.setOpenId(openConfigDTO.getId());
                openAuthDO.setOpenAppId(openConfigDTO.getOpenAppId());
                openAuthDO.setComponentAppId(openConfigDTO.getComponentAppId());
            }
            OpenAuthOfExtend openAuthOfExtend = param.getOpenAuthOfExtend();
            if (null != openAuthOfExtend) {
                openAuthDO.setExtendJson(JsonUtils.toJsonStr(openAuthOfExtend));
            }
            openAuthDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            openAuthDO.setTenantId(param.getTenantId());
            if (null != openAuthId) {
                openAuthDO.setId(openAuthId);
                openAuthDO.setModifyBy(UserUtils.getUserId());
                openAuthDO.setModifyTime(LocalDateTime.now());
                LogUtils.info("更新钉钉三方应用授权参数:{}", JSON.toJSONString(openAuthDO));

                openAuthMapper.updateById(openAuthDO);
                if(null != openAuthOfExtend) {
                    OpenAuthOfAliDingTalkTp openAuthOfAliDingTalkTp = openAuthOfExtend.getOpenAuthOfAliDingTalkTp();
                    if(openAuthOfAliDingTalkTp!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfAliDing openConfigOfAliDing = new OpenConfigOfAliDing();
                        openConfigOfAliDing.setComponentVerifyTicket(openAuthOfAliDingTalkTp.getSuiteTicket());
                        openConfigOfAliDing.setComponentAccessToken(openAuthOfAliDingTalkTp.getSuiteAccessToken());
                        openConfigOfAliDing.setComponentAccessTokenExpireIn(openAuthOfAliDingTalkTp.getSuiteAccessExpirein());
                        openConfigOfAliDing.setComponentAccessTokenExpireTimes(openAuthOfAliDingTalkTp.getSuiteAccessExpireTime());
                        openConfigOfExtend.setOpenConfigOfAliDing(openConfigOfAliDing);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            } else {
                // 确保一个租户下只有一个授权小程序/公众号
                OpenAuthDO insOpenAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                        .eq(OpenAuthDO::getTenantId, param.getTenantId())
                        .eq(OpenAuthDO::getConfigType, param.getConfigType())
                        .eq(OpenAuthDO::getConfigSubType, param.getConfigSubType())
                        .eq(OpenAuthDO::getAppType, param.getAppType()));
                if(null != insOpenAuthDO){
                    throw new BusinessException("一个租户只能安装一个三方助手！");
                }
                openAuthDO.setCreateBy(UserUtils.getUserId());
                openAuthDO.setCreateTime(LocalDateTime.now());
                openAuthDO.setModifyTime(LocalDateTime.now());
                openAuthDO.setId(IdGenerator.getId());
                LogUtils.info("插入开放平台授权参数:{}", JSON.toJSONString(openAuthDO));
                openAuthMapper.insert(openAuthDO);
                if(openAuthOfExtend != null) {
                    OpenAuthOfAliDingTalkTp openAuthOfAliDingTalkTp = openAuthOfExtend.getOpenAuthOfAliDingTalkTp();
                    if(openAuthOfAliDingTalkTp!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfAliDing openConfigOfAliDing = new OpenConfigOfAliDing();
                        openConfigOfAliDing.setComponentVerifyTicket(openAuthOfAliDingTalkTp.getSuiteTicket());
                        openConfigOfAliDing.setComponentAccessToken(openAuthOfAliDingTalkTp.getSuiteAccessToken());
                        openConfigOfAliDing.setComponentAccessTokenExpireIn(openAuthOfAliDingTalkTp.getSuiteAccessExpirein());
                        openConfigOfAliDing.setComponentAccessTokenExpireTimes(openAuthOfAliDingTalkTp.getSuiteAccessExpireTime());
                        openConfigOfExtend.setOpenConfigOfAliDing(openConfigOfAliDing);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            }
            return true;
        }
        return false;
    }

    private Boolean saveAndUpdateOpenAuthOfAliDingTalkSb(OpenAuthParam param) {
        if(param!=null){
            Long openAuthId = null;
            Integer configType = param.getConfigType();
            Integer configSubType = param.getConfigSubType();
            Integer serviceAppType = param.getAppType();
            if (redisUtils.tryLock(AUTH_LOCK.concat("dt:cp:sb:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()))) {
                //查询是否已经存在
                LogUtils.info("查询缓存中是否存在授权参数:{}", JSON.toJSONString(param));
                try {
                    OpenAuthDO openAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                            .eq(OpenAuthDO::getTenantId, param.getTenantId())
                            .eq(OpenAuthDO::getOpenAppId, param.getOpenAppId())
                            .eq(OpenAuthDO::getComponentAppId, param.getComponentAppId())
                    );
                    if (null != openAuthDO) {
                        openAuthId = openAuthDO.getId();
                    }
                } finally {
                    redisUtils.unlock(AUTH_LOCK.concat("dt:cp:sb:").concat(param.getOpenAppId()).concat(":").concat(param.getComponentAppId()).concat(":").concat(param.getTenantId().toString()));
                }
            }
            OpenAuthDO openAuthDO = BeanUtil.convertBean(param, OpenAuthDO.class);
            OpenConfigQuery configQuery = new OpenConfigQuery();
            configQuery.setType(configType);
            configQuery.setSubType(configSubType);
            OpenConfigDTO openConfigDTO = wxOpenConfigServiceImpl.queryOpenConfigOne(configQuery);
            if (null == openConfigDTO) {
                throw new BusinessException("未配置开放平台参数");
            }
            if(StringUtils.isBlank(openAuthDO.getOpenAppId()) || (openAuthDO.getOpenId() == null)) {
                openAuthDO.setOpenId(openConfigDTO.getId());
                openAuthDO.setOpenAppId(openConfigDTO.getOpenAppId());
                openAuthDO.setComponentAppId(openConfigDTO.getComponentAppId());
            }
            OpenAuthOfExtend openAuthOfExtend = param.getOpenAuthOfExtend();
            if (null != openAuthOfExtend) {
                openAuthDO.setExtendJson(JsonUtils.toJsonStr(openAuthOfExtend));
            }
            openAuthDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            openAuthDO.setTenantId(param.getTenantId());
            if (null != openAuthId) {
                openAuthDO.setId(openAuthId);
                openAuthDO.setModifyBy(UserUtils.getUserId());
                openAuthDO.setModifyTime(LocalDateTime.now());
                LogUtils.info("更新钉钉应用授权参数:{}", JSON.toJSONString(openAuthDO));

                openAuthMapper.updateById(openAuthDO);
                if(null != openAuthOfExtend) {
                    OpenAuthOfAliDingTalkSb openAuthOfAliDingTalkSb = openAuthOfExtend.getOpenAuthOfAliDingTalkSb();
                    if(openAuthOfAliDingTalkSb!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfAliDing openConfigOfAliDing = new OpenConfigOfAliDing();
                        openConfigOfAliDing.setComponentAccessToken(openAuthOfAliDingTalkSb.getAccessToken());
                        openConfigOfAliDing.setComponentAccessTokenExpireIn(openAuthOfAliDingTalkSb.getAccessTokenExpiresIn());
                        openConfigOfAliDing.setComponentAccessTokenExpireTimes(openAuthOfAliDingTalkSb.getAccessTokenExpiresTime());
                        openConfigOfExtend.setOpenConfigOfAliDing(openConfigOfAliDing);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            } else {
                // 确保一个租户下只有一个授权小程序/公众号
                OpenAuthDO insOpenAuthDO = openAuthMapper.selectOne(new LambdaQueryWrapper<>(OpenAuthDO.class)
                        .eq(OpenAuthDO::getTenantId, param.getTenantId())
                        .eq(OpenAuthDO::getConfigType, param.getConfigType())
                        .eq(OpenAuthDO::getConfigSubType, param.getConfigSubType())
                        .eq(OpenAuthDO::getAppType, param.getAppType()));
                if(null != insOpenAuthDO){
                    throw new BusinessException("一个租户只能授权一个小程序/公众号！");
                }
                openAuthDO.setCreateBy(UserUtils.getUserId());
                openAuthDO.setCreateTime(LocalDateTime.now());
                openAuthDO.setModifyTime(LocalDateTime.now());
                openAuthDO.setId(IdGenerator.getId());
                LogUtils.info("插入开放平台授权参数:{}", JSON.toJSONString(openAuthDO));
                openAuthMapper.insert(openAuthDO);
                if(openAuthOfExtend != null) {
                    OpenAuthOfAliDingTalkSb openAuthOfAliDingTalkSb = openAuthOfExtend.getOpenAuthOfAliDingTalkSb();
                    if(openAuthOfAliDingTalkSb!=null) {
                        OpenConfigOfExtend openConfigOfExtend = new OpenConfigOfExtend();
                        OpenConfigOfAliDing openConfigOfAliDing = new OpenConfigOfAliDing();
                        openConfigOfAliDing.setComponentAccessToken(openAuthOfAliDingTalkSb.getAccessToken());
                        openConfigOfAliDing.setComponentAccessTokenExpireIn(openAuthOfAliDingTalkSb.getAccessTokenExpiresIn());
                        openConfigOfAliDing.setComponentAccessTokenExpireTimes(openAuthOfAliDingTalkSb.getAccessTokenExpiresTime());
                        openConfigOfExtend.setOpenConfigOfAliDing(openConfigOfAliDing);
                        OpenConfigDO openConfigDO = openConfigMapper.selectById(openConfigDTO.getId());
                        openConfigDO.setExtendJson(JsonUtils.toJsonStr(openConfigOfExtend));
                        openConfigMapper.updateById(openConfigDO);
                    }
                }
            }
            return true;
        }
        return false;
    }

    private OpenAuthDTO transferOpenAuthDTO(OpenAuthDO openAuthDO) {
        if(openAuthDO!=null){
            Integer type = openAuthDO.getConfigType();
            Integer subType = openAuthDO.getConfigSubType();
            Integer serviceType = openAuthDO.getAppType();
            if(type== OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_OPEN.getCode()){
                if( subType == WxOpenAppTypeEnum.WECHAT_OPEN_APP_TYPE_TP.getCode()) {
                    if(serviceType == WxOpenServiceTypeConstant.APPID_TYPE_MP) {
                        return transferOpenAuthOfWxMp(openAuthDO);
                    }else if(serviceType== WxOpenServiceTypeConstant.APPID_TYPE_MINI){
                        return transferOpenAuthOfWxMa(openAuthDO);
                    }
                }
            }else if(type== OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode()){
                if( subType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_TP.getCode()) {
                    return transferOpenAuthOfWxCpTpDTO(openAuthDO);
                }else if( subType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_DK.getCode()) {
                    return transferOpenAuthOfWxCpDkDTO(openAuthDO);
                }if( subType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_SB.getCode()) {
                    return transferOpenAuthOfWxCpSbDTO(openAuthDO);
                }if( subType == WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_CONTACT.getCode()) {
                    return transferOpenAuthOfWxCpContactDTO(openAuthDO);
                }
            }else if (type == OpenApiConfigEnum.OPEN_CONFIG_TYPE_DINGTALK_OPEN.getCode()) {
                if(subType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_WEB.getCode()) {
                    return transferOpenAuthOfAliDingTalkTpDTO(openAuthDO);
                }else if(subType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_MA.getCode()) {
                    return transferOpenAuthOfAliDingTalkTpDTO(openAuthDO);
                }else if(subType == AliDingTalkAppTypeEnum.ALIDINGTALK_CORP_APP_TYPE_WEB.getCode()) {
                    return transferOpenAuthOfAliDingTalkSbDTO(openAuthDO);
                }else if(subType == AliDingTalkAppTypeEnum.ALIDINGTALK_CORP_APP_TYPE_MA.getCode()) {
                    return transferOpenAuthOfAliDingTalkSbDTO(openAuthDO);
                }else if(subType == AliDingTalkAppTypeEnum.ALIDINGTALK_TP_APP_TYPE_PERSON.getCode()) {
                    return transferOpenAuthOfAliDingTalkTpDTO(openAuthDO);
                }
            }
        }
        return null;
    }

    private OpenAuthDTO transferOpenAuthOfWxMp(OpenAuthDO openAuthDO) {
        OpenAuthDTO openAuthDTO = BeanUtil.convertBean(openAuthDO, OpenAuthDTO.class);
        String extendJson = openAuthDO.getExtendJson();
        if(StringUtils.isNotEmpty(extendJson)) {
            OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
            if(openAuthOfExtend!=null) {
                OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
                openAuthDTO.setCorpName(openAuthOfWxOpen.getNickName());
                openAuthOfWxOpen.setVerifyTypeInfoDesc(VerifyTypeInfoEnum.getName(openAuthOfWxOpen.getVerifyTypeInfo()));
                openAuthDTO.setHasOpenAuth(openAuthOfWxOpen.getAuthorizerAccessToken() != null);
                openAuthDTO.setOpenAuthOfExtend(openAuthOfExtend);
            }
            return openAuthDTO;
        }else{
            return null;
        }
    }

    private OpenAuthDTO transferOpenAuthOfWxMa(OpenAuthDO openAuthDO) {
        OpenAuthDTO openAuthDTO = BeanUtil.convertBean(openAuthDO, OpenAuthDTO.class);
        String extendJson = openAuthDO.getExtendJson();
        if(StringUtils.isNotEmpty(extendJson)) {
            OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
            if(openAuthOfExtend!=null) {
                OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
                OpenAuthOfWxOpenMaProgram wxOpenAuthProgramDTO = openAuthOfWxOpen.getProgramParam();
                if (wxOpenAuthProgramDTO == null) {
                    wxOpenAuthProgramDTO = new OpenAuthOfWxOpenMaProgram();
                }
                openAuthDTO.setCorpName(openAuthOfWxOpen.getNickName());
                List<OpenAuthOfWxOpenMaProgramCategory> wxOpenAuthProgramCategoryDTOS = wxOpenAuthProgramDTO.getCategories();
                if (Collections3.isNotEmpty(wxOpenAuthProgramCategoryDTOS)) {
                    wxOpenAuthProgramDTO.setCategories(wxOpenAuthProgramCategoryDTOS);
                    openAuthOfWxOpen.setCategoryNames(wxOpenAuthProgramCategoryDTOS.stream().map(x -> x.getFirst() + ">" + x.getSecond()).collect(Collectors.toList()));
                }
                openAuthOfWxOpen.setVerifyTypeInfoDesc(VerifyTypeInfoEnum.getName(openAuthOfWxOpen.getVerifyTypeInfo()));
                openAuthDTO.setHasOpenAuth(openAuthOfWxOpen.getAuthorizerAccessToken() != null);
                openAuthDTO.setOpenAuthOfExtend(openAuthOfExtend);
            }
            return openAuthDTO;
        }else{
            return null;
        }
    }

    private OpenAuthDTO transferOpenAuthOfWxCpTpDTO(OpenAuthDO openAuthDO) {
        OpenAuthDTO openAuthDTO = BeanUtil.convertBean(openAuthDO, OpenAuthDTO.class);
        String extendJson = openAuthDO.getExtendJson();
        if(StringUtils.isNotEmpty(extendJson)) {
            OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
            if(openAuthOfExtend!=null){
                OpenAuthOfWxCpTp openAuthOfWxCpTp = openAuthOfExtend.getOpenAuthOfWxCpTp();
                if(openAuthOfWxCpTp!=null){
                    OpenAuthOfWxCpTpAuthCorpInfo authCorpInfo = openAuthOfWxCpTp.getAuthCorpInfo();
                    if(authCorpInfo!=null){
                        openAuthDTO.setCorpName(authCorpInfo.getCorpName());
                    }
                    openAuthDTO.setHasOpenAuth(openAuthOfWxCpTp.getCorpAccessToken() != null);
                }
            }

            openAuthDTO.setOpenAuthOfExtend(openAuthOfExtend);
            return openAuthDTO;
        }else{
            return null;
        }
    }

    private OpenAuthDTO transferOpenAuthOfWxCpDkDTO(OpenAuthDO openAuthDO) {
        OpenAuthDTO openAuthDTO = BeanUtil.convertBean(openAuthDO, OpenAuthDTO.class);
        String extendJson = openAuthDO.getExtendJson();
        if(StringUtils.isNotEmpty(extendJson)) {
            OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
            if(openAuthOfExtend!=null){
                OpenAuthOfWxCpDk openAuthOfWxCpDk = openAuthOfExtend.getOpenAuthOfWxCpDk();
                if(openAuthOfWxCpDk!=null){
                    OpenAuthOfWxCpTpAuthCorpInfo authCorpInfo = openAuthOfWxCpDk.getAuthCorpInfo();
                    if(authCorpInfo!=null){
                        openAuthDTO.setCorpName(authCorpInfo.getCorpName());
                    }
                    openAuthDTO.setHasOpenAuth(openAuthOfWxCpDk.getProviderAccessToken() != null);
                }
            }
            openAuthDTO.setOpenAuthOfExtend(openAuthOfExtend);
            return openAuthDTO;
        }else{
            return null;
        }
    }

    private OpenAuthDTO transferOpenAuthOfWxCpSbDTO(OpenAuthDO openAuthDO) {
        OpenAuthDTO openAuthDTO = BeanUtil.convertBean(openAuthDO, OpenAuthDTO.class);
        String extendJson = openAuthDO.getExtendJson();
        if(StringUtils.isNotEmpty(extendJson)) {
            OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
            openAuthDTO.setOpenAuthOfExtend(openAuthOfExtend);
            return openAuthDTO;
        }else{
            return null;
        }
    }

    private OpenAuthDTO transferOpenAuthOfWxCpContactDTO(OpenAuthDO openAuthDO) {
        OpenAuthDTO openAuthDTO = BeanUtil.convertBean(openAuthDO, OpenAuthDTO.class);
        String extendJson = openAuthDO.getExtendJson();
        if(StringUtils.isNotEmpty(extendJson)) {
            OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
            if(openAuthOfExtend!=null){
                OpenAuthOfWxCpContact openAuthOfWxCpContact = openAuthOfExtend.getOpenAuthOfWxCpContact();
                if(openAuthOfWxCpContact!=null){
                    openAuthDTO.setHasOpenAuth(openAuthOfWxCpContact.getCorpAccessToken() != null);
                }
            }

            openAuthDTO.setOpenAuthOfExtend(openAuthOfExtend);
            return openAuthDTO;
        }else{
            return null;
        }
    }

    private OpenAuthDTO transferOpenAuthOfAliDingTalkTpDTO(OpenAuthDO openAuthDO) {
        OpenAuthDTO openAuthDTO = BeanUtil.convertBean(openAuthDO, OpenAuthDTO.class);
        String extendJson = openAuthDO.getExtendJson();
        if(StringUtils.isNotEmpty(extendJson)) {
            OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
            if(openAuthOfExtend!=null){
                OpenAuthOfAliDingTalkTp openAuthOfAliDingTalkTp = openAuthOfExtend.getOpenAuthOfAliDingTalkTp();
                if(openAuthOfAliDingTalkTp!=null){
                    OpenAuthOfAliDingTalkTpAuthCorpInfo authCorpInfo = openAuthOfAliDingTalkTp.getAuthCorpInfo();
                    if(authCorpInfo!=null){
                        openAuthDTO.setCorpName(authCorpInfo.getCorpName());
                    }
                    openAuthDTO.setHasOpenAuth((openAuthOfAliDingTalkTp.getAccessToken() != null) && openAuthOfAliDingTalkTp.getSuiteAccessToken() != null);
                }
            }

            openAuthDTO.setOpenAuthOfExtend(openAuthOfExtend);
            return openAuthDTO;
        }else{
            return null;
        }
    }

    private OpenAuthDTO transferOpenAuthOfAliDingTalkSbDTO(OpenAuthDO openAuthDO) {
        OpenAuthDTO openAuthDTO = BeanUtil.convertBean(openAuthDO, OpenAuthDTO.class);
        String extendJson = openAuthDO.getExtendJson();
        if(StringUtils.isNotEmpty(extendJson)) {
            OpenAuthOfExtend openAuthOfExtend = JsonUtils.fromJson(extendJson, OpenAuthOfExtend.class);
            openAuthDTO.setOpenAuthOfExtend(openAuthOfExtend);
            return openAuthDTO;
        }else{
            return null;
        }
    }

    private IOpenAuthService getBean(){
        return SpringBeanUtil.getBean(OpenAuthServiceImpl.class);
    }
}
